clear 

* set directory 
* cd "directory"

* set paths
global datafolder "data"
global outputfolder "output"

********************************************************************************
*			Figure 6: Actual rate changes by initial BE rate
********************************************************************************
	use "$datafolder\maindata.dta", clear
		
	* Keep only borrowers who have 2015 observation
	gen date2015m1m12 = inrange(datem,660,671)
	bysort customerid: egen has_2015m1m12 = max(date2015m1m12)
	
	keep if has_2015m1m12==1
	
	sort customerid datem
	
	* Define initial theoretical rates to sort on
	gen thr_pre_x = theoretical_rate if inrange(datem,660,671)
	bysort customerid: egen thr_pre = mean(thr_pre_x)
	
	gen ThR_bin = .
	replace ThR_bin = 1 if thr_pre < 4.6
	replace ThR_bin = 2 if thr_pre >= 4.6 & thr_pre <= 6
	replace ThR_bin = 3 if thr_pre > 6
	
	bysort customerid year: egen ar_yr = mean(actual_rate)
	
	gen ar_2014_x = ar_yr if year==2014
	bysort customerid: egen ar_2014 = mean(ar_2014_x)
	drop ar_2014_x
	
	gen ar_2015_x = ar_yr if year==2015
	bysort customerid: egen ar_2015 = mean(ar_2015_x)
	drop ar_2015_x
	
	gen dif_ar_1415 = ar_2015 - ar_2014
	
	* Drop customers who do not have 2014 to compare to 2015
	drop if dif_ar_1415 == .
	
	* Hist of 2014 vs 2015 dif by ThR bin
	collapse (mean) dif_ar_1415 ThR_bin, by(customerid)
	
	quietly sum dif_ar_1415 if ThR_bin==1,d
	local dif_mean `: di %3.2f r(mean)'
	local dif_sd `: di %3.2f r(sd)'
	local ub = `dif_mean' + `dif_sd'
	local lb = `dif_mean' - `dif_sd'
	
	* Low group
	hist dif_ar_1415 if ThR_bin==1, bin(20) frequency color(gray%40) ///
	graphregion(color(white)) xscale(range(-6(2)8)) xlabel(#10) ///
	yscale(range(0(2)10)) ylabel(#10, angle(horizontal)) ///
	xtitle("Change in actual rate") title("Low", color(black)) ///
	xline(`dif_mean', lpattern(dash)) ///
	xline(`lb', lpattern(dash) lcolor(gray)) ///
	xline(`ub', lpattern(dash) lcolor(gray))
	
	graph export "$outputfolder\figure_6_L.pdf", ///
	as(pdf) replace
	graph export "$outputfolder\figure_6_L.png", ///
	as(png) replace
	
	quietly sum dif_ar_1415 if ThR_bin==2,d
	local dif_mean `: di %3.2f r(mean)'
	local dif_sd `: di %3.2f r(sd)'
	local ub = `dif_mean' + `dif_sd'
	local lb = `dif_mean' - `dif_sd'
	
	* Med group
	hist dif_ar_1415 if ThR_bin==2, bin(20) frequency color(gray%40) ///
	graphregion(color(white)) xscale(range(-6(2)8)) xlabel(#10) ///
	yscale(range(0(2)10)) ylabel(#10, angle(horizontal)) ///
	xtitle("Change in actual rate") title("Med", color(black)) ///
	xline(`dif_mean', lpattern(dash)) ///
	xline(`lb', lpattern(dash) lcolor(gray)) ///
	xline(`ub', lpattern(dash) lcolor(gray))
	
	graph export "$outputfolder\figure_6_M.pdf", ///
	as(pdf) replace
	graph export "$outputfolder\figure_6_M.png", ///
	as(png) replace
	
	quietly sum dif_ar_1415 if ThR_bin==3,d
	local dif_mean `: di %3.2f r(mean)'
	local dif_sd `: di %3.2f r(sd)'
	local ub = `dif_mean' + `dif_sd'
	local lb = `dif_mean' - `dif_sd'
	
	* High group
	hist dif_ar_1415 if ThR_bin==3, bin(10) frequency color(gray%40) ///
	graphregion(color(white)) xscale(range(-6(2)8)) xlabel(#10) ///
	yscale(range(0(2)10)) ylabel(#10, angle(horizontal)) ///
	xtitle("Change in actual rate") title("High", color(black)) ///
	xline(`dif_mean', lpattern(dash)) ///
	xline(`lb', lpattern(dash) lcolor(gray)) ///
	xline(`ub', lpattern(dash) lcolor(gray))
	
	graph export "$outputfolder\figure_6_H.pdf", ///
	as(pdf) replace
	graph export "$outputfolder\figure_6_H.png", ///
	as(png) replace
	
	